#ifndef CMAINAPP_H_
#define CMAINAPP_H_

#include <iostream>
#include <QObject>

#include "CommonType.h"
#include "IPlatformProxyInterface.h"
#include "IPayplatInterface.h"
#include "IValidatorInterface.h"

class HomeWnd;
class MainWnd;
class CBackendWnd;
class CAdWnd;
class QStackedWidget;
class QLabel;
class CResourceManager;
class QTimer;

class CMainApp :
        public QObject,
        public zl::IPlatformProxyObserver,
        public zl::IPayplatObserver,
        public zl::IValidatorObserver
{
	Q_OBJECT
public:
	CMainApp();
	~CMainApp();

	int32_t Run();

    /// 实现管理台回复数据回调函数
    inline int32_t EventPlatReply(const zl::PairParams& data) override {
        emit sigPlatReply(data);
        return 0;
    }
    /// 实现支付平台回复回调函数
    inline void PayplatReply(zl::PayReply rep, int32_t code) override {
        emit sigPayplatReply(rep, code);
    }
    /// 纸币器设备事件回调函数
    inline void EventCredit(int32_t value, const std::string& currency) override {
        emit sigITLEventCredit(value, currency.c_str());
    }

protected:
	void init_view();
	void init_configuration();
	void init_logger();
	void init_database();
    void init_cabinet();
	void init_platform_proxy();
	void init_audio_broadcast();
	void init_resources_manager();
    void init_cabint_devices();

    void set_current_view(int index);
	void load_label_pixmap(QLabel* lb, const QString& filename);
    void exit_application();

    void auto_upload_unconfirmed();

    void auto_upload_unconfirmed_operlog();
	void auto_upload_unconfirmed_orders();
    void auto_upload_unconfirmed_paid_details();
    void update_app_configuration();

	bool eventFilter(QObject *watched, QEvent *event) override;
	void timerEvent(QTimerEvent* e) override;

signals:
    void sigPlatReply(const zl::PairParams& data);
    void sigPayplatReply(zl::PayReply rep, int code);
    void sigITLEventCredit(int valud, QString currency);

private slots:
	void event_verify_manager_card(QString cardno);
	void event_back_to_home();
	void event_config_changed();
	void event_cab_locker_status_changed(int32_t locker);
	void event_resource_load_finished(int type, bool flag, QString filename);
	void event_resource_load_progress(int type, qint64 cur, qint64 total);
	void event_install_finished();
    void event_plat_reply(const zl::PairParams& data);
    void event_pay_plat_reply(zl::PayReply rep, int code);
    void event_itl_event_credit(int value, QString currency);

public:
    int32_t					timer_status_;
    int32_t					timer_heartbeat_;
	CResourceManager*		res_mgr_;

	QStackedWidget*			container_;
	CAdWnd*					ad_wnd_;
	HomeWnd*				home_wnd_;
	MainWnd*				main_wnd_;
    CBackendWnd*			backend_wnd_;

    zl::IPlatformProxyInterface*    plat_api_;
    zl::IValidatorInterface*        validator_api_;
};

#endif // CMAINAPP_H_+
